﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Best.SmartCms.Core.Utility;
using Best.SmartCms.Utility;
using Microsoft.Extensions.DependencyInjection;

namespace Best.SmartCms.Service.Common
{
    /// <summary>
    /// 服务统一接口
    /// </summary>
    public interface IService
    {
    }

    /// <summary>
    /// 服务注册
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 注入Sevice
        /// </summary>
        /// <param name="service"></param>
        /// <returns></returns>
        public static IServiceCollection AddServices(this IServiceCollection service)
        {
            var services = Assembly.GetAssembly(typeof(IService)).GetTypes()
                .Where(type => !type.IsInterface && typeof(IService).IsAssignableFrom(type)).ToList();
            foreach (var type in services)
            {
                service.AddScoped(type);
            }
            NLogManager.LogInfo($"ServiceCollectionExtensions AddServices: {services.Count} services added.");
            return service;
        }
    }
}
